package com.ssf.runningerrands.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.ssf.runningerrands.R;
import com.ssf.runningerrands.entity.Building;
import com.ssf.runningerrands.views.multichoiceadapter.MultiChoiceBaseAdapter;

public class MyRuningFragment extends ListFragment implements
		OnItemClickListener {

	private MyBaseAdapter adapter;

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		setListShown(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		rebuildList(savedInstanceState);
	}

	private void rebuildList(Bundle savedInstanceState) {
		List<Building> items = Building.createList(getActivity());
		adapter = new MyBaseAdapter(savedInstanceState, items);
		adapter.setOnItemClickListener(this);
		adapter.setAdapterView(getListView());
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub

	}

	public class MyBaseAdapter extends MultiChoiceBaseAdapter implements
			OnClickListener {

		private List<Building> items = new ArrayList<Building>();

		public MyBaseAdapter(Bundle savedInstanceState, List<Building> items) {
			super(savedInstanceState);
			this.items = items;
		}

		@Override
		protected View getViewImpl(int position, View convertView,
				ViewGroup parent) {
			if (convertView == null) {
				int layout = R.layout.run_list_item;
				LayoutInflater inflater = LayoutInflater.from(getContext());
				convertView = inflater.inflate(layout, parent, false);
			}
			ViewGroup group = (ViewGroup) convertView;
			Building building = getItem(position);
			((TextView) group.findViewById(android.R.id.text1))
					.setText(building.name);
			((TextView) group.findViewById(R.id.textbutton))
					.setOnClickListener(this);
			return group;
		}

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			MenuInflater inflater = mode.getMenuInflater();
			inflater.inflate(R.menu.discard, menu);
			return true;
		}

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			if (item.getItemId() == R.id.menu_discard) {
				discardSelectedItems();
				return true;
			}
			return false;
		}

		public void discardSelectedItems() {
			List<Building> mItems = new ArrayList<Building>();
			Set<Long> selection = getCheckedItems();
			for (long position : selection) {
				mItems.add(getItem((int)position));
			}
			for (Building building : mItems) {
				items.remove(building);
			}
			notifyDataSetChanged();
			finishActionMode();
		}

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Building getItem(int position) {
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return false;
		}

		@Override
		public void onClick(View v) {
//			Toast.makeText(getContext(), ((TextView) v).getText(),
//					Toast.LENGTH_SHORT).show();
		}
	}
}
